package com.soul.sys.controller;

import com.soul.common.bean.vo.MenuTreeVo;
import com.soul.common.controller.BaseController;
import com.soul.common.msg.ApiResult;
import com.soul.sys.bean.dto.MenuSearchDto;
import com.soul.sys.bean.dto.menu.MenuAddDto;
import com.soul.sys.bean.dto.menu.MenuUpdateDto;
import com.soul.sys.bean.vo.RouteVo;
import com.soul.sys.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author sws
 * @since 2023-09-01 22:47:08
 */
@RestController
@RequestMapping("/menu")
@Api(value="menu", tags = "菜单相关接口")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;


    @GetMapping("/tree")
    @ApiOperation(value = "获取树形菜单")
    public ApiResult<List<MenuTreeVo>> queryMenuTree(MenuSearchDto searchForm){
        List<MenuTreeVo> menuTreeVos = menuService.queryMenuTree(searchForm);
        return ApiResult.success(menuTreeVos);
    }

    @GetMapping("/parent/list")
    @ApiOperation(value = "获取树形菜单")
    public ApiResult<List<MenuTreeVo>> getParentMenu(MenuSearchDto searchForm){
        List<MenuTreeVo> menuTreeVos = menuService.getParentMenu(searchForm);
        return ApiResult.success(menuTreeVos);
    }

    /**
     * 新增菜单
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增菜单")
    public ApiResult<?> addMenu(@RequestBody MenuAddDto menuAddDto){
        menuService.addMenu(menuAddDto);
        return ApiResult.success();
    }

    /**
     * 修改菜单
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改菜单信息")
    public ApiResult<?> updateMenu(@RequestBody MenuUpdateDto menuUpdateDto){
        menuService.updateMenu(menuUpdateDto);
        return ApiResult.success();
    }


    /**
     * 删除菜单
     * @return
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除菜单")
    public ApiResult<?> delMenu(@PathVariable("id") Long id){
        menuService.deleteMenu(id);
        return ApiResult.success();
    }



    @GetMapping("/routes")
    @ApiOperation(value = "获取路由树形")
    public ApiResult<List<RouteVo>> queryRoutesTree(){
        List<RouteVo> menuTreeVos = menuService.queryRoutesTree();
        return ApiResult.success(menuTreeVos);
    }
}
